package com.smart.community.region.controller;

import com.smart.community.commons.annotation.ApiLog;
import com.smart.community.commons.enums.ApiLogOperationType;
import com.smart.community.commons.result.Result;
import com.smart.community.region.dto.BatchCreateFeeStandardDTO;
import com.smart.community.region.service.IShopFeeStandardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 店铺费用标准管理控制器
 * 
 * @author Wu.Liang
 * @version 1.0.0
 * @since 2025-01-30
 */
@Slf4j
@RestController
@RequestMapping("/region/shop-fee-standards")
@Tag(name = "店铺费用标准管理", description = "店铺费用标准管理相关接口")
public class ShopFeeStandardController {
    
    @Autowired
    private IShopFeeStandardService shopFeeStandardService;
    
    /**
     * 批量新增费用标准
     * 遵循《Java后端SpringBoot代码开发规范_重构版.md》Controller规范
     * 遵循《系统状态枚举权威标准声明.md》状态枚举规范
     */
    @ApiLog(
        logTitle = "批量新增费用标准",
        logType = 2,
        moduleName = "区域管理",
        operationType = ApiLogOperationType.ADD
    )
    @PostMapping("/batch")
    @Operation(summary = "批量新增费用标准", description = "为多个店铺批量设置费用标准")
    public Result<List<Long>> batchCreateFeeStandard(
            @Parameter(description = "批量新增费用标准信息") @RequestBody @Valid BatchCreateFeeStandardDTO batchCreateDTO) throws Exception {
        
        log.info("开始批量新增费用标准，店铺数量: {}, 费用类型: {}", 
                batchCreateDTO.getShopIds().size(), batchCreateDTO.getFeeType());
        
        // 调用Service层进行批量创建
        List<Long> createdIds = shopFeeStandardService.batchCreateFeeStandard(batchCreateDTO);
        
        log.info("批量新增费用标准成功，创建数量: {}", createdIds.size());
        return Result.success("批量新增费用标准成功", createdIds);
    }
    
    /**
     * 分页查询费用标准
     * 遵循《Java后端SpringBoot代码开发规范_重构版.md》Controller规范
     * 遵循《系统状态枚举权威标准声明.md》状态枚举规范
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询费用标准", description = "根据条件分页查询费用标准列表")
    public Result<Object> getFeeStandardPage(
            @Parameter(description = "当前页", example = "1") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "店铺ID") @RequestParam(required = false) Long shopId,
            @Parameter(description = "费用类型") @RequestParam(required = false) String feeType,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "费用标准金额范围-最小值") @RequestParam(required = false) java.math.BigDecimal minFeeAmount,
            @Parameter(description = "费用标准金额范围-最大值") @RequestParam(required = false) java.math.BigDecimal maxFeeAmount,
            @Parameter(description = "生效日期开始") @RequestParam(required = false) java.time.LocalDate effectiveDateStart,
            @Parameter(description = "生效日期结束") @RequestParam(required = false) java.time.LocalDate effectiveDateEnd,
            @Parameter(description = "过期日期开始") @RequestParam(required = false) java.time.LocalDate expiryDateStart,
            @Parameter(description = "过期日期结束") @RequestParam(required = false) java.time.LocalDate expiryDateEnd,
            @Parameter(description = "创建时间开始") @RequestParam(required = false) java.time.LocalDateTime createTimeStart,
            @Parameter(description = "创建时间结束") @RequestParam(required = false) java.time.LocalDateTime createTimeEnd,
            @Parameter(description = "关键字搜索") @RequestParam(required = false) String keyword) throws Exception {
        
        log.info("分页查询费用标准，当前页: {}, 每页大小: {}, 店铺ID: {}, 费用类型: {}, 状态: {}, 费用金额范围: {}-{}, 生效日期范围: {}-{}, 过期日期范围: {}-{}, 创建时间范围: {}-{}, 关键字: {}", 
                current, size, shopId, feeType, status, minFeeAmount, maxFeeAmount, 
                effectiveDateStart, effectiveDateEnd, expiryDateStart, expiryDateEnd, 
                createTimeStart, createTimeEnd, keyword);
        
        // 调用Service层进行分页查询
        Object result = shopFeeStandardService.getFeeStandardPage(current, size, shopId, feeType, status,
                minFeeAmount, maxFeeAmount, effectiveDateStart, effectiveDateEnd, 
                expiryDateStart, expiryDateEnd, createTimeStart, createTimeEnd, keyword);
        
        log.info("分页查询费用标准成功");
        return Result.success(result);
    }
    
    /**
     * 根据ID获取费用标准详情
     * 遵循《Java后端SpringBoot代码开发规范_重构版.md》Controller规范
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取费用标准详情", description = "根据ID获取费用标准详细信息")
    public Result<Object> getFeeStandardDetail(
            @Parameter(description = "费用标准ID") @PathVariable Long id) throws Exception {
        
        log.info("获取费用标准详情，ID: {}", id);
        
        // 调用Service层获取详情
        Object result = shopFeeStandardService.getFeeStandardDetail(id);
        
        if (result == null) {
            log.warn("费用标准不存在，ID: {}", id);
            return Result.error("费用标准不存在");
        }
        
        log.info("获取费用标准详情成功，ID: {}", id);
        return Result.success(result);
    }
    
    /**
     * 更新费用标准
     * 遵循《Java后端SpringBoot代码开发规范_重构版.md》Controller规范
     */
    @ApiLog(
        logTitle = "更新费用标准",
        logType = 2,
        moduleName = "区域管理",
        operationType = ApiLogOperationType.UPDATE
    )
    @PutMapping("/{id}")
    @Operation(summary = "更新费用标准", description = "更新指定ID的费用标准信息")
    public Result<Boolean> updateFeeStandard(
            @Parameter(description = "费用标准ID") @PathVariable Long id,
            @Parameter(description = "费用标准信息") @RequestBody @Valid BatchCreateFeeStandardDTO updateDTO) throws Exception {
        
        log.info("更新费用标准，ID: {}", id);
        
        // 调用Service层进行更新
        Boolean result = shopFeeStandardService.updateFeeStandard(id, updateDTO);
        
        if (result) {
            log.info("更新费用标准成功，ID: {}", id);
            return Result.success("更新费用标准成功", true);
        } else {
            log.warn("更新费用标准失败，ID: {}", id);
            return Result.error("更新费用标准失败");
        }
    }
    
    /**
     * 删除费用标准
     * 遵循《Java后端SpringBoot代码开发规范_重构版.md》Controller规范
     */
    @ApiLog(
        logTitle = "删除费用标准",
        logType = 2,
        moduleName = "区域管理",
        operationType = ApiLogOperationType.DELETE
    )
    @DeleteMapping("/{id}")
    @Operation(summary = "删除费用标准", description = "删除指定ID的费用标准")
    public Result<Boolean> deleteFeeStandard(
            @Parameter(description = "费用标准ID") @PathVariable Long id) throws Exception {
        
        log.info("删除费用标准，ID: {}", id);
        
        // 调用Service层进行删除
        Boolean result = shopFeeStandardService.deleteFeeStandard(id);
        
        if (result) {
            log.info("删除费用标准成功，ID: {}", id);
            return Result.success("删除费用标准成功", true);
        } else {
            log.warn("删除费用标准失败，ID: {}", id);
            return Result.error("删除费用标准失败");
        }
    }
    
    /**
     * 批量删除费用标准
     * 遵循《Java后端SpringBoot代码开发规范_重构版.md》Controller规范
     */
    @ApiLog(
        logTitle = "批量删除费用标准",
        logType = 2,
        moduleName = "区域管理",
        operationType = ApiLogOperationType.DELETE
    )
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除费用标准", description = "批量删除指定的费用标准")
    public Result<Boolean> batchDeleteFeeStandard(
            @Parameter(description = "费用标准ID列表") @RequestBody List<Long> ids) throws Exception {
        
        log.info("批量删除费用标准，数量: {}", ids.size());
        
        if (ids == null || ids.isEmpty()) {
            return Result.error("费用标准ID列表不能为空");
        }
        
        // 调用Service层进行批量删除
        Boolean result = shopFeeStandardService.batchDeleteFeeStandard(ids);
        
        if (result) {
            log.info("批量删除费用标准成功，数量: {}", ids.size());
            return Result.success("批量删除费用标准成功", true);
        } else {
            log.warn("批量删除费用标准失败，数量: {}", ids.size());
            return Result.error("批量删除费用标准失败");
        }
    }
}
